package com.fit.pages;

import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.beans.CarBean;
import com.fit.beans.DriverBean;
import com.fit.beans.EmployerBean;
import com.fit.beans.ReservationBean;
import com.fit.interfaces.Borrow;
import com.fit.interfaces.Car;
import com.fit.interfaces.Driver;
import com.fit.interfaces.Employer;
import com.fit.interfaces.Reservation;
import com.fit.interfaces.User;
import com.fit.service.Cars;
import com.fit.service.Drivers;
import com.fit.service.Employers;
import com.fit.service.Reservations;

public class EntryReservation {

	@SessionState
	private User user;
	
	private boolean userExists;

	public User getUser() {
		return user;
	}

	Object onActivate(){
		if(!userExists) return Login.class;
		return null;
	}
	
	@Persist
	private String idOfRes;
	
	@Persist
	private Date dateOfR;
	
	@Persist
	private String reservedCar;
	
	@Persist
	private String reservedDriver;
	
	@Persist
	private String reservedEmployer;

	

	public String getIdOfRes() {
		return idOfRes;
	}

	public void setIdOfRes(String idOfRes) {
		this.idOfRes = idOfRes;
	}

	public Date getDateOfR() {
		return dateOfR;
	}

	public void setDateOfR(Date dateOfR) {
		this.dateOfR = dateOfR;
	}

	public String getReservedCar() {
		return reservedCar;
	}

	public void setReservedCar(String reservedCar) {
		this.reservedCar = reservedCar;
	}

	public String getReservedDriver() {
		return reservedDriver;
	}

	public void setReservedDriver(String reservedDriver) {
		this.reservedDriver = reservedDriver;
	}

	public String getReservedEmployer() {
		return reservedEmployer;
	}

	public void setReservedEmployer(String reservedEmployer) {
		this.reservedEmployer = reservedEmployer;
	}

	
	
	@Inject
	private Reservations reservations;
	
	@Inject
	private Cars cars;
	
	@Inject
	private Drivers drivers;
	
	@Inject 
	private Employers employers;
	
	public Collection<Car> getCars(){
		return cars.retrieve();
	}
	
	public Collection<Driver> getDrivers(){
		return drivers.retrive();
	}
	
	public Collection<Employer> getEmployers(){
		return employers.retrive();
	}
	
	@InjectPage
	private ViewReservation viewReservationPage;
	
	@InjectPage
	private ViewError viewErrorPage;

//	@SetupRender
//	public void createObject(){
////		driver = new DriverBean();
////		driver.setBorrow(new LinkedList<Borrow>());
//		 driver = new DriverBean();
//		driver.setReservation(new LinkedList<Reservation>());
//		
//		 employer = new EmployerBean();
//		 employer.setReservation(new LinkedList<Reservation>());
//		 
//		 car = new CarBean();
//		 car.setReservation(new LinkedList<Reservation>());
//	}
	
	@OnEvent
	public Object onSubmit(){
		reservedEmployer = "WebApp";
		reservedDriver = getUser().getUserName();
		
		Car car = null;
		Driver driver = null;
		Employer employer = null;
		
		try {
			car = cars.retriveSingleCar(getReservedCar());
			driver = drivers.retriveSingleDriver(getReservedDriver());
			employer = employers.retriveSingleEmployer(getReservedEmployer());
		} catch (Exception e) {
			viewErrorPage.setPassedMessage(e.toString());
			return viewErrorPage;
		}
		
		if(driver == null){
			viewErrorPage.setPassedMessage("Error in recognition object Drivers in Map's");
			return viewErrorPage;
		}
		if(employer == null){
			viewErrorPage.setPassedMessage("Error in recognition object Employers in Map's");
			return viewErrorPage;
		}
		if(car == null){
			viewErrorPage.setPassedMessage("Error in recognition object Cars in Map's");
			return viewErrorPage;
		}
		
		Reservation reservationBean = new ReservationBean();
		reservationBean.executeReservation(dateOfR, employer, driver, car);
		
		reservations.save(reservationBean);
		System.out.println("ima objekata u listu:" + reservations.retrive().size());
		idOfRes = "";
		dateOfR = null;
		reservedCar = "";
		reservedDriver = "";
		reservedEmployer = "";
		return viewReservationPage;
		
	}
}
